package org.shj.spark.operator;

import java.util.Arrays;
import java.util.List;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.VoidFunction;

public class MapOperator {

	public static void main(String[] args) {
		SparkConf conf = new SparkConf().setMaster("local").setAppName("MapOperator");
		JavaSparkContext ctx = new JavaSparkContext(conf);
		
		List<Integer> numbers = Arrays.asList(1,2,3,4,5);
		JavaRDD<Integer> numberRDD = ctx.parallelize(numbers);
		
		//map 对每个元素进行操作
		JavaRDD<Integer> mapResults = numberRDD.map(new Function<Integer,Integer>(){
			private static final long serialVersionUID = -7683890113299050533L;

			@Override
			public Integer call(Integer v1) throws Exception {
				// TODO Auto-generated method stub
				return v1 * 10;
			}
		});
		
		mapResults.foreach(new VoidFunction<Integer>(){
			private static final long serialVersionUID = 1L;

			@Override
			public void call(Integer t) throws Exception {
				System.out.println(t);
			}
		});
		
		ctx.close();

	}

}
